package com.dingqb.view.myview;

import android.content.Context;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.View;

import com.dingqb.mycs.R;

public class CircleView extends View implements View.OnClickListener {

	// 计数值，每点击一次本控件，其值增加1
	private int mCount = 0;

	// 用于获取文字的宽和高
	private Rect mBounds;

	public CircleView(Context context, AttributeSet attrs) {
		super(context, attrs);
		mBounds = new Rect();
		setOnClickListener(this);
	}

	/**
	 * 要画图形，最起码要有三个对象： 1.颜色对象 Color 2.画笔对象 Paint 3.画布对象 Canvas
	 */
	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		Paint paint = new Paint();
		paint.setColor(Color.BLUE);
		// 设置字体大小
		paint.setTextSize(100);
		// 设置画出的线的 粗细程度
		paint.setStrokeWidth(5);
		canvas.drawLine(0, 0, 200, 200, paint);
		// 画矩形
		canvas.drawRect(200, 500, 300, 300, paint);
		// 画圆
		canvas.drawCircle(200, 200, 100, paint);
		// 画出字符串 drawText(String text, float x, float y, Paint paint)
		// y 是 基准线 ，不是 字符串的 底部
		canvas.drawText("apple", 60, 60, paint);
		canvas.drawLine(0, 60, 500, 60, paint);
		// 绘制图片
		canvas.drawBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher), 150, 150, paint);

		String text = "点击次数：" + String.valueOf(mCount);
		// 获取文字的宽和高
		paint.getTextBounds(text, 0, text.length(), mBounds);
		float textWidth = mBounds.width();
		float textHeight = mBounds.height();

		// 绘制字符串
		canvas.drawText(text, getWidth() / 2 - textWidth / 2, getHeight() / 2 + textHeight / 2, paint);
	}

	@Override
	public void onClick(View v) {
		mCount++;
		// 重绘
		invalidate();

	}

}
